package com.kaifamiao.month05.day18;

import org.junit.Before;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class DBUtilTest {

    private DBUtil db;


    @Before
    public void init() {
        db = new DBUtil();
    }

    @Test
    public void i() {
        db = new DBUtil();
        System.out.println(db);
    }

    @Test
    public void register() {
        db.getConnection();
        String user = "张三";
        String pass = "123456";
        String sql = "insert into test(id,name,pass) values(test_ID.NEXTVAL,?,?)";
        int r = db.executeUpdate(sql, user, pass);
        if (r > 0) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败！");
        }
        db.close();
    }


    @Test
    public void login() throws SQLException {
        db.getConnection();
        System.out.println("请输入用户名：");
        Scanner in = new Scanner(System.in);
        String user = in.next();
        System.out.println("请输入密码：");
        String pass = in.next();
        String sql = "select * from test where name = ? and pass = ?";
        ResultSet set = db.executeQuery(sql,user,pass);
        if (set.next()) {
            System.out.println("登录成功！");
        } else {
            System.out.println("登录失败！");
        }
    }
    @Test
    public void selectOne() {
        User user = db.selectOne(User.class,"select * from users");
        System.out.println(user);
    }
}
